package com.example.easynote;



import com.example.easynote.R;

import DAO.AlbumsController;
import DAO.PhotosData;
import DTO.AlbumModel;
import Help.Manager.Album.AlbumsFragment;
import Help.Manager.Album.App;
import Help.Manager.Album.PhotosGroupedFragment;
import Help.Manager.Album.TabListener;
import android.app.ActionBar;
import android.app.Activity;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView.AdapterContextMenuInfo;

/*
 * MainActivity
 * Hiển thị giao diện gồm ActionBar với 2 tab Albums và Date.
 * Tab Albums hiển thị Grid các Album có trong máy.
 * Tab Date hiển thị tất cả các Photo có trong máy, group theo Tháng/Năm của Photo.
 */
public class MainActivity extends Activity {

	AlbumsFragment albumsFragment;
	PhotosGroupedFragment groupedFragment;
	PhotosData photosData;
	AlbumsController albumsCtrl;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		this.photosData = App.getPhotosData();
		this.albumsCtrl = App.getAlbumsController();
		
		try {
			this.photosData.loadPhotosFromExternalStorage();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return;
		}
		
		
		this.albumsCtrl.createAlbums(this.photosData.getPhotos());
		
		this.albumsFragment = new AlbumsFragment(this.albumsCtrl.getAlbums());
		this.groupedFragment = new PhotosGroupedFragment();
		
		this.buildActionBar();
	}

	@Override
	public void onResume() {
		super.onResume();
		
		this.albumsFragment.update();
	}
	
	public void buildActionBar() {
		ActionBar bar = getActionBar();


		bar.addTab(bar
				.newTab()
				.setText(R.string.albums)
				.setTabListener(
						new TabListener<AlbumsFragment>(
								this, 
								"f1", 
								this.albumsFragment)));
		bar.addTab(bar
				.newTab()
				.setText(R.string.date)
				.setTabListener(
						new TabListener<PhotosGroupedFragment>(
								this, 
								"f2", 
								this.groupedFragment)));
		
		bar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM
				|             ActionBar.DISPLAY_USE_LOGO);

		bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

		bar.setDisplayShowHomeEnabled(true);
		bar.setDisplayShowTitleEnabled(true);
		
		bar.show();
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		//getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
	
	@Override
	public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
		AdapterContextMenuInfo cmi = (AdapterContextMenuInfo) menuInfo;

		int id = v.getId();
		if(id == R.id.gridview_albums){
			int position = cmi.position;
			menu.setHeaderTitle(this.albumsCtrl.getAlbum(position).getTitle());
			menu.add(0, position, 0, R.string.delete_album);
		}
	}
	
	@Override
	public boolean onContextItemSelected(MenuItem item) {
		int id = item.getGroupId();
		if(id == 0) {
			int position = item.getItemId();
			
			AlbumModel album = this.albumsCtrl.getAlbum(position);
			this.photosData.removeAlbum(albumsCtrl, album);
			
			this.albumsFragment.update();
		}

		return true;
	}

}
